//User.java  
//
//Copyright (C) 2008 Devon Mizelle  
//  
//This program is distributed in the hope that it will be useful,  
//but WITHOUT ANY WARRANTY; without even the implied warranty of  
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  
//GNU General Public License for more details.  
//  
//You should have received a copy of the GNU General Public License  
//along with this program.  If not, see <http://www.gnu.org/licenses/>.

package hermes;
import java.net.InetAddress;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;

/**
 *
 * @author Devon Mizelle
 */
public class User implements java.io.Serializable {
    private String id;
    private KeyPair kp = null;
    private KeyPairGenerator kpg = null;
    private PublicKey publicKey;
    private PrivateKey privateKey;
    private String ip = null;
    
    public User(String id){
        this.id = id;
        generateKeys();
        try{
            InetAddress add = InetAddress.getLocalHost();
            ip = add.getHostAddress();
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }
    
    public User(String id, PublicKey publicKey, PrivateKey privateKey){
        this.id = id;
        this.publicKey = publicKey;
        this.privateKey = privateKey;
    }
   
    public Object get(String get){
        if(get.equals("id")){
            return id;
        }
        if(get.equals("publicKey")){
            return publicKey;
        }
        if(get.equals("privateKey")){
            return privateKey; //@todo Restrict sniffers from getting private key.
        }
        if(get.equals("ip")){
            return ip;
        }
        return ("Invalid parameter " + get + " in getting for user " + id + ".");
    }
    
    public void generateKeys(){
        java.security.Security.addProvider(new com.sun.crypto.provider.SunJCE());
        try{
            kpg = KeyPairGenerator.getInstance("DSA", "SUN");
            SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG", "SUN");
            kpg.initialize(1024, secureRandom);
            kp = kpg.genKeyPair();
            publicKey = kp.getPublic();
            privateKey = kp.getPrivate();
            kpg = null;
        }
        catch(Exception e){
            e.printStackTrace();
        }
        
        
    }
}
